package jeumpapayment;

import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;

import jeumpapayment.record.UserData;

public class JeumpaPayment extends UiApplication {
	public static JeumpaPayment JP;
	public static final int SCREEN_WIDTH = BBScreen.Gemini.WIDTH;
	public static final int SCREEN_HEIGHT = BBScreen.Gemini.HEIGHT;
	public static final Bitmap BANNER = Bitmap.getBitmapResource("banner_gemini.png");
	
	/**
	 * Main function
	 * @param args Arguments for main function
	 */
	public static void main(String args[]){
		
		JP = new JeumpaPayment();
		JP.enterEventDispatcher();
				
	}
	
	/**
	 * Application constructor. Load first screen of the application.
	 */
	public JeumpaPayment(){
		//******************************************************************************************************
		//Note: This part is added to solve ControlledAccessException that is faced when clicking
		//      Menu button in some screens
		
		ApplicationPermissionsManager manager = ApplicationPermissionsManager.getInstance();
		if (manager.getPermission(ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION) != ApplicationPermissions.VALUE_ALLOW) {
			ApplicationPermissions permissions = manager.getApplicationPermissions();
			permissions.addPermission(ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION);
			manager.invokePermissionsRequest(permissions);
		}
		
		//******************************************************************************************************
		
		UserData userData = new UserData();
		try{
			userData.loadRecord();
		}
		catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		
		if(userData.getPhonenumber() == null)
			switchScreen(null,new RegisterScreen());
		else
			switchScreen(null,new LoginScreen());
	}
	
	/**
	 * Switch from one screen to another 
	 * @param current Screen to be switches. Pass null if the screen needs to be stacked not switched (not closing current screen).
	 * @param next Screen that will appear.
	 */
	public static void switchScreen(Screen current, Screen next){
		if(current != null)
			UiApplication.getUiApplication().popScreen(current);
		
		UiApplication.getUiApplication().pushScreen(next);
	}
}
